<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\admin\model;

use app\models\BaseModel;
use think\Exception;
use think\Log;
use think\Model;

class BaoxiaoModel extends BaseModel
{

    protected $type = [
        'more' => 'array',
    ];
    const BAOXIAO_USE = [1=>'话费支出',2=>'技术支出',3=>'行政支出'];

    const FINISH_REVIEW_STATUS = 1;//完成
    const CEO_REVIEW_STATUS = 2;//经理审核
    const AD_REVIEW_STATUS = 3;//行政审核
    const FD_REVIEW_STATUS = 4;//财务审核
    const REFUSED_REVIEW_STATUS = 5;//驳回

    public function getListById($page,$limit,$role_id,$cycle_id=0){
        try {
            $where = [];
            $array = [1,2,3,4];//超级管理员，财务、行政、总经理
            $employee_id = session('EMPLOYEE_ID');
            if(!in_array($role_id,$array)){
                $where['b.employee_id'] = $employee_id;
            }
            if($cycle_id!=0){
                $where['b.cycle_id'] = $cycle_id;
            }
            return $this->alias('b')->where($where)->leftJoin('employee e','b.employee_id=e.id')->field('b.*,e.name')->limit(($page-1)*$limit,$limit)->select();
        }catch (Exception $exception){
            $this->setErrorInfo($exception->getMessage());
        }
    }

    /**
     * @param $role_id
     * @param $cycle_id
     * @return float|int|string
     */
    public function getCount($role_id,$cycle_id=0)
    {
        $where = [];
        $array = [1,2,3,4];//超级管理员，财务、行政、总经理
        $employee_id = session('EMPLOYEE_ID');
        if(!in_array($role_id,$array)){
            $where['employee_id'] = $employee_id;
        }
        if($cycle_id!=0){
            $where['cycle_id'] = $cycle_id;
        }
        return $this->where($where)->count();
    }


    /**
     * 获取待审核列表
     * @param $role_id
     * @return array|\PDOStatement|string|\think\Collection|\think\model\Collection
     */
    public function getReviewList($role_id)
    {
        try{
            switch ($role_id){
                case 2://经理审核
                    $where[] = ['b.status','=',self::CEO_REVIEW_STATUS];
                    break;
                case 3://行政放款
                    $where[] = ['b.status','=',self::AD_REVIEW_STATUS];
                    break;
                case 4://财务审核
                    $where[] = ['b.status','=',self::FD_REVIEW_STATUS];
                    break;
                default://所有待审核
                    $where[] = ['b.status','in',[self::CEO_REVIEW_STATUS,self::AD_REVIEW_STATUS,self::FD_REVIEW_STATUS]];
                    break;
            }
            return $this->alias('b')->where($where)->leftJoin('employee e','b.employee_id=e.id')->field('b.*,e.name')->select();
        }catch (\Exception $exception){
            self::setErrorInfo($exception->getMessage());
        }
    }

    /**
     * 获取待审核数量
     * @param $role_id
     * @return float|int|string
     */
    public function getReviewCount($role_id)
    {
        switch ($role_id){
            case 2://经理审核
                $where[] = ['status','=',self::CEO_REVIEW_STATUS];
                break;
            case 3://行政放款
                $where[] = ['status','=',self::AD_REVIEW_STATUS];
                break;
            case 4://财务审核
                $where[] = ['status','=',self::FD_REVIEW_STATUS];
                break;
            default://所有待审核
                $where[] = ['status','in',[self::CEO_REVIEW_STATUS,self::AD_REVIEW_STATUS,self::FD_REVIEW_STATUS]];
                break;
        }
        return $this->where($where)->count();
    }

    /**
     * 报销审核
     * @param $id
     * @param $type
     * @param $role_id
     * @return int|string
     */
    public function doReview($id,$type,$role_id)
    {
        try{
            if($type==1){
                switch ($role_id){
                    case 2://经理审核通过
                        $status = 3;
                        break;
                    case 3://所有审核完成
                        $status = 1;
                        break;
                    case 4://财务审核通过
                        $status = 2;
                        break;
                    default://待审核
                        throw new Exception('当前角色没有权限！');
                        break;
                }
            }else{//驳回
                $status = 5;
            }
            if(is_array($id)){
                $where[] = ['id','in',$id];
            }else{
                $where[] = ['id','=',$id];
            }
            return $this->where($where)->setField('status',$status);
        }catch (Exception $exception){
            self::setErrorInfo($exception->getMessage());
        }
    }



    /**
     * 获取报销信息
     * @param $id
     * @return array|\PDOStatement|string|Model|null
     */
    public static function getInfo($id)
    {
        try {
            $info = self::where('id',$id)->find();
            if(!empty($info['img'])){
                $info['img'] = explode(',',$info['img']);
            }
            return $info;
        }catch (\Exception $exception){
            self::setErrorInfo($exception->getMessage());
        }
    }

    /**
     * 编辑信息
     * @param $id
     * @param $data
     * @return BaoxiaoModel
     */
    public function edit($id,$data)
    {
        return $this->update($data,['id'=>$id]);
    }

}